package leetcode_2401_2500;

import java.util.Arrays;

public class LeeCode_2488_timeout {
    public static void main(String[] args) {
//        System.out.println(countSubarrays(new int[]{3, 2, 1, 4, 5}, 4));
//        System.out.println(countSubarrays(new int[]{2,3,1}, 3));
        System.out.println(countSubarrays(new int[]{5,19,11,15,13,16,4,6,2,7,10,8,18,20,1,3,17,9,12,14},
        6));
    }
    private static int countSubarrays(int[] nums, int k) {
        int index = 0;
        int res = 0;
        int len = nums.length;
        for (index = 0; index < len; index++) {
            if (nums[index] == k)
                break;
        }
        if (index == len)
            return 0;
        for (int i = 0; i < len; i++) {
            for (int s = index - i; s <= index; s++) {
                if (s + i + 1 > len || s < 0){
                    continue;
                }
                int[] son = Arrays.copyOfRange(nums, s, s + i + 1);
                Arrays.sort(son);
                if (son[(i) / 2] == k){
                    res++;
                }
            }
        }
        return res;
    }
}
